package service;

import java.util.List;

import dao.CartDao;
import dao.ProductDao;
import pojo.Cart;
import pojo.PageBean;
import pojo.Product;

public class CartService {
	CartDao cdao;
	ProductDao pdao;
	public PageBean<Product> show(int currentPage,int pageSize) {
		pdao=new ProductDao();
		PageBean<Product> pb = new PageBean<Product>();
		pb.setList(pdao.PageData(currentPage, pageSize));
		pb.setTotal(pdao.getTotal());
		int totalPage=(pdao.getTotal()%pageSize==0)?(pdao.getTotal()/pageSize):(pdao.getTotal()/pageSize+1);
		pb.setPageSize(pageSize);
		pb.setCurrentPage(currentPage);
		pb.setTotalPage(totalPage);
		return pb;
	}
	//编辑数量
	public int editCart(int cid,int num) {
		int flag=0;
		cdao=new CartDao();
		flag=cdao.updateNumByCID(cid, num);
		return flag;
	}
	//删除功能
	public int delCart(String cid) {
		int flag=0;
		cdao=new CartDao();
		flag=cdao.delCartByCID(cid);
		return flag;
	}
	//购买功能
	public int buyCart(Cart c) {
		int flag=0;
		cdao = new CartDao();
		Cart cart = cdao.getCartByUIDandPID(c);//根据用户名和商品编号判断cart表中是否有这个商品
		if(cart==null) {
			flag=cdao.insertCart(c);
		}else {
			flag=cdao.updateNumByUIDandPID(cart);
		}
		return flag;
	}
	//根据用户显示商品
	public List<Cart> showCartsByUID(String uid){
		cdao = new CartDao();
		List<Cart> list=null;
		list=cdao.showCarts(uid);
		return list;
	}
	public List<Cart> getOneCart(int c_id) {
		cdao =new CartDao();
		List<Cart> list=null;
		list=cdao.getOneCart(c_id);
		return list;
	}
}
